package com.itheima;


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.sql.DataSource;
import java.util.Date;
import java.util.Set;

@SpringBootTest
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Demo {


    @Autowired
    private  RedisTemplate redisTemplate;

      @Test  //  Object  Object    jdkSerializationRedisSerializer    默认序列化对象
    public  void  test1(){
//  JdkSerializationRedisSerializer
          Date date = new Date();

          redisTemplate.opsForSet().add("aa","cc","dd","tom");
          redisTemplate.opsForSet().add("bb",date);

          Set aa = redisTemplate.opsForSet().members("aa");
          Set bb = redisTemplate.opsForSet().members("bb");
          System.out.println(aa);
          System.out.println(bb);

          System.out.println(redisTemplate.getKeySerializer()+"====="+redisTemplate.getValueSerializer());

      }



    @Test  //  存储对象的
    public  void  test2(){

          //  重写 序列化对象  key  value
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Date>(Date.class)); // 存储对象

        Date date = new Date();
//        redisTemplate.opsForSet().add("aa","cc","dd","tom");
        redisTemplate.opsForSet().add("bb",date);

//        Set aa = redisTemplate.opsForSet().members("aa");
        Set bb = redisTemplate.opsForSet().members("bb");
//        System.out.println(aa);
        System.out.println(bb);

        System.out.println(redisTemplate.getKeySerializer()+"====="+redisTemplate.getValueSerializer());

        redisTemplate.delete("bb"); //   删除指定key 对应得数据

    }

    @Test  //  存储字符串类型数据
    public  void  test3(){

        //  重写 序列化对象  key  value
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.opsForSet().add("aa","cc","dd","tom");

        Set aa = redisTemplate.opsForSet().members("aa");
        System.out.println(aa);

        System.out.println(redisTemplate.getKeySerializer()+"====="+redisTemplate.getValueSerializer());

    }


    @Test
    public  void  test4(){
//   引入配置类定义 序列化对象

        redisTemplate.opsForSet().add("aa","cc","dd","tom");
        redisTemplate.opsForSet().add("bb",new Date());

        Set aa = redisTemplate.opsForSet().members("aa");
        Set bb = redisTemplate.opsForSet().members("bb");
        System.out.println(aa);
        System.out.println(bb);

        System.out.println(redisTemplate.getKeySerializer()+"====="+redisTemplate.getValueSerializer());

    }
}
